# firefox completion                                       -*- shell-script -*-

_firefox()
{
    local cur prev words cword split
    _init_completion -s || return

    [[ $cur == -MOZ_LOG*=* ]] && prev=${cur%%=*} cur=${cur#*=}

    case $prev in
        --help | --version | --display | --UILocale | -MOZ_LOG | --new-window | --new-tab | \
            --private-window | --window-size | --search | --start-debugger-server | \
            --recording | --debugger-args | -[hva])
            return
            ;;
        -P)
            COMPREPLY=($(compgen -W "$(
                awk -F= '$1 == "Name" { print $2 }' \
                    ~/.mozilla/firefox/profiles.ini 2>/dev/null
            )" -- "$cur"))
            return
            ;;
        --profile | --screenshot)
            _filedir -d
            return
            ;;
        -MOZ_LOG_FILE)
            _filedir log
            return
            ;;
        --recording-output | --recording-file)
            _filedir
            return
            ;;
        --debugger | -d)
            COMPREPLY=($(compgen -c -- "$cur"))
            return
            ;;
    esac

    $split && return

    if [[ $cur == -* ]]; then
        COMPREPLY=($(compgen -W '$(_parse_help "$1")' -- "$cur"))
        [[ ${COMPREPLY-} == *= ]] && compopt -o nospace
        return
    fi

    _filedir "@(?([xs])htm?(l)|pdf|txt)"
} &&
    complete -F _firefox firefox firefox-esr iceweasel mozilla-firefox

# ex: filetype=sh
